package com.emperor.product.app;

import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.emperor.productlog.logic.ProductLog;
import com.swtdesigner.SWTResourceManager;

public class ProductAppMgmRmk extends Dialog {

	private Combo combo_1;
	private Combo combo;
	protected Object result;
	protected Shell shell;
	protected String stockOptions;
	protected String actionName;
	protected Map<String, Object> params;
	protected Boolean greater = false;
	
	public ProductAppMgmRmk(Shell parent, int style, Map<String, Object> params, boolean greater) {
		super(parent, style);
		this.params = params;
		this.greater = greater;
	}
	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public ProductAppMgmRmk(Shell parent, int style) {
		super(parent, style);
	}

	/**
	 * Create the dialog
	 * @param parent
	 */
	public ProductAppMgmRmk(Shell parent) {
		this(parent, SWT.NONE);
	}

	/**
	 * Open the dialog
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		return result;
	}

	/**
	 * Create contents of the dialog
	 */
	protected void createContents() {
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		shell.setBackgroundImage(SWTResourceManager.getImage(ProductAppMgmRmk.class, "/img/main.jpg"));
		shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
		shell.setImage(SWTResourceManager.getImage(ProductAppMgmRmk.class, "/img/iMac OSX.png"));
		shell.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		shell.setSize(500, 375);
		shell.setText("Product Update Quantity");

		centreShell(shell);
		
		final Composite composite = new Composite(shell, SWT.NONE);
		composite.setBounds(0, 0, 494, 312);

		final Label statusLabel = new Label(composite, SWT.NONE);
		statusLabel.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		statusLabel.setText("Status");
		statusLabel.setBounds(36, 123, 50, 20);

		combo = new Combo(composite, SWT.READ_ONLY);
		combo.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		combo.setBounds(110, 120, 318, 28);
		combo.add(ProductLog.ACT_STOCK_IN);
		combo.add(ProductLog.ACT_STOCK_OUT);
		if (greater) {
			combo.select(0);
		} else {
			combo.select(1);
		}

		combo_1 = new Combo(composite, SWT.NONE);
		combo_1.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		combo_1.setBounds(110, 154, 318, 28);
		combo_1.add(ProductLog.ACT_NEW);
		combo_1.add(ProductLog.ACT_SPOIL);
		combo_1.add(ProductLog.ACT_CHANGE);

		final Label statusLabel_1 = new Label(composite, SWT.NONE);
		statusLabel_1.setBounds(36, 157, 60, 20);
		statusLabel_1.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		statusLabel_1.setText("Remark");

		final Button okButton = new Button(composite, SWT.NONE);
		okButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent e) {
				if (combo.getText().length() > 0 && combo_1.getText().length() > 0) {
					getParams().put(ProductAppCtrl.PRODUCT_ACTIONNAME, combo.getText() + "-" + combo_1.getText());
					getShell().close();
				}
			}
		});
		okButton.setImage(SWTResourceManager.getImage(ProductAppMgmRmk.class, "/img/Symbol-Check.png"));
		okButton.setFont(SWTResourceManager.getFont("", 12, SWT.NONE));
		okButton.setText("OK");
		okButton.setBounds(110, 200, 60, 30);
		//
	}

	public String getStockOptions() {
		return stockOptions;
	}

	public void setStockOptions(String stockOptions) {
		if (stockOptions != null) {
			if (stockOptions instanceof String) {
				this.stockOptions = (String) stockOptions;
			} else {
				try {
					this.stockOptions = stockOptions.toString();
				} catch (Exception e) {
				}
			}
		}
	}

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		if (actionName != null) {
			if (actionName instanceof String) {
				this.actionName = (String) actionName;
			} else {
				try {
					this.actionName = actionName.toString();
				} catch (Exception e) {
				}
			}
		}
	}

	public Map<String, Object> getParams() {
		return params;
	}

	public void setParams(Map<String, Object> params) {
		this.params = params;
	}
	public Shell getShell() {
		return shell;
	}

	public void centreShell(Shell shell) {
		Rectangle pDisplayBounds = shell.getDisplay().getBounds();
		Rectangle pShellBounds = shell.getBounds();

		int nLeft = (pDisplayBounds.width - pShellBounds.width) / 2;
		int nTop = (pDisplayBounds.height - pShellBounds.height) / 2;

		shell.setBounds(nLeft, nTop, pShellBounds.width, pShellBounds.height);
	}
}
